package test.controllers;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.lightsomy.annotations.Action;
import com.google.code.lightsomy.annotations.BeforeAction;
import com.google.code.lightsomy.annotations.Controller;
import com.google.code.lightsomy.output.Renderer;

@Controller
public class Index {
	
	private Renderer velocity;
	
	private Renderer freeMarker;

	@BeforeAction
	public void prepare(HttpServletRequest req, HttpServletResponse res) {
		velocity = Renderer.getRenderer(Renderer.Type.VELOCITY, req, res);
		freeMarker = Renderer.getRenderer(Renderer.Type.FREE_MARKER, req, res);
	}
	
	@Action
	public void index(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.getWriter()
			.append(req.getParameter("controller"))
			.append(req.getParameter("action"))
			.flush();
	}
	
	@Action(route="^/some/funky/stuff/(<mister>\\d+)$")
	public void funkyMister(HttpServletRequest req, HttpServletResponse res) throws IOException {
		res.getWriter()
			.append(req.getParameter("controller"))
			.append(req.getParameter("action"))
			.append(req.getParameter("mister"))
			.flush();
	}
	
	@Action
	public void velocity(HttpServletRequest req, HttpServletResponse res) {
		velocity.set("variable", "123");
		velocity.output(velocity.render());
	}
	
	@Action
	public void velocityExplicit(HttpServletRequest req, HttpServletResponse res) {
		velocity.set("variable", "123");
		velocity.output(velocity.render("/view/index/velocity.tpl"));
	}
	
	@Action(layout="/view/velocity-layout.tpl")
	public void velocityLayout(HttpServletRequest req, HttpServletResponse res) {
		velocity.set("title", "layout");
		velocity.set("variable", "123");
		velocity.output(velocity.render("/view/index/velocity.tpl"));
	}
	
	@Action
	public void freemarker(HttpServletRequest req, HttpServletResponse res) {
		freeMarker.set("variable", "123");
		freeMarker.output(freeMarker.render());
	}
	
	@Action
	public void freemarkerExplicit(HttpServletRequest req, HttpServletResponse res) {
		freeMarker.set("variable", "123");
		freeMarker.output(freeMarker.render("/view/index/freemarker.tpl"));
	}
	
	@Action(layout="/view/freemarker-layout.tpl")
	public void freemarkerLayout(HttpServletRequest req, HttpServletResponse res) {
		freeMarker.set("title", "layout");
		freeMarker.set("variable", "123");
		freeMarker.output(freeMarker.render("/view/index/freemarker.tpl"));
	}
}
